/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "FileCache.h"
#include "CameraLauncher.h"
#include "CordovaViewController.h"
#include "cJSON.h"
#include <filemanagement/file_uri/oh_file_uri.h>


REGISTER_PLUGIN_CLASS(CameraLauncher)
bool CameraLauncher::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "takePicture") {
        m_cbc = cbc;
        CordovaArgs cordovaArgs(args);
        m_nQuality = cordovaArgs.getInt(0);
        m_nDestType = cordovaArgs.getInt(1);
        m_nSrcType = cordovaArgs.getInt(2);
        int nTargetWidth = cordovaArgs.getInt(3);
        int nTargetHeight = cordovaArgs.getInt(4);
        int nEncodingType = cordovaArgs.getInt(5);
        m_nMediaType = cordovaArgs.getInt(6);
        bool bAllowEdit = cordovaArgs.getBoolean(7); //已淘汰，默认允许编辑
        bool bCorrectOrientation = cordovaArgs.getBoolean(8);
        bool bSaveToPhotoAlbum = cordovaArgs.getBoolean(9); //鸿蒙默认保存，不能修改
        int nPopoverOptions = cordovaArgs.getInt(10); //iOS专用
        int nCameraDirection = cordovaArgs.getInt(11);
        
        string strPageIndex = "";
        if(m_nSrcType == CAMERA) {
            strPageIndex = "./ImageCompress/CameraAction/StartCamera";
        } else if(m_nSrcType == PHOTO_LIBRARY || m_nSrcType == SAVED_PHOTO_ALBUM) {
            strPageIndex = "./MediaAction/MediaAction/SelectMedia";
        }
        
        bool isCameraImageCompress = true;
        if(!bAllowEdit) {
            isCameraImageCompress = false;
        }
        long lngCompressImageSize = 4*1024*1024;
        string strCameraCompressShowToast;
        ((CordovaViewController*)Application::g_cordovaViewController)->getCameraImageCompress(isCameraImageCompress, lngCompressImageSize, strCameraCompressShowToast);
        cJSON* pJson = cJSON_CreateObject();
        cJSON_AddNumberToObject(pJson, "Quality", m_nQuality);
        cJSON_AddNumberToObject(pJson, "TargetWidth", nTargetWidth);
        cJSON_AddNumberToObject(pJson, "TargetHeight", nTargetHeight);
        cJSON_AddNumberToObject(pJson, "EncodingType", nEncodingType);
        cJSON_AddNumberToObject(pJson, "MediaType", m_nMediaType);
        cJSON_AddBoolToObject(pJson, "CorrectOrientation", bCorrectOrientation);
        cJSON_AddNumberToObject(pJson, "CameraDirection", nCameraDirection);
        if(isCameraImageCompress)
            cJSON_AddTrueToObject(pJson, "CameraImageCompress");
        else
            cJSON_AddFalseToObject(pJson, "CameraImageCompress");
        cJSON_AddNumberToObject(pJson, "CompressImageSize", lngCompressImageSize);
        cJSON_AddStringToObject(pJson, "CameraCompressShowToast", strCameraCompressShowToast.c_str());
        
        char* pPageArgs = cJSON_Print(pJson);
        executeArkTs(strPageIndex, m_nMediaType, pPageArgs, "Camera", cbc);
        cJSON_Delete(pJson);
        free(pPageArgs);
        return true;
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    return true;
}

bool CameraLauncher::onArKTsResult(cJSON* args) 
{
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(json != NULL && json->type == cJSON_Array) {
        int count = cJSON_GetArraySize(json);
        if(count > 0) {
            if(m_nDestType == DATA_URL) {
                char* result = cJSON_GetArrayItem(json, 0)->valuestring;
                if(result != NULL && strlen(result) > 0) {
                    long lngFileSize = FileCache::getFileSize(result);
                    char* pBuf = (char*)malloc(lngFileSize);
                    if(FileCache::readFile(result, (unsigned char*)pBuf, lngFileSize)) {
                        string strResult = CBase64::encode(pBuf, lngFileSize);
                        m_cbc.success(strResult);
                    }
                    free(pBuf);    
                } else {
                     m_cbc.error("No Image Selected");
                }
            }
            if(m_nDestType == FILE_URL) {
                char* result = cJSON_GetArrayItem(json, 0)->valuestring;
                if(result != NULL && strlen(result) > 0) {
                    string strFilePath = result;
                    if(strFilePath.find("file://") == 0) {
                        char *pathResult = NULL;
                        FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(strFilePath.c_str(), strFilePath.length(), &pathResult);
                        if (ret == 0 && pathResult != NULL) {
                            strFilePath = pathResult;
                            free(pathResult);
                        }
                    }
                    strFilePath = "https://localhost" + strFilePath;
                    m_cbc.success(strFilePath);
                } else {
                    m_cbc.error("No Image Selected");
                }
            }
        } else {
            m_cbc.error("No Image Selected");
        }
    }
    return true;
}